<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('分公司新增')"/>
</head>
<style>
    .layui-input{height:30px;} /* layui-input 默认太高了，这里固定高度*/
    label.required:after {   /* 给红色的必填项打红色星号（文字后）如果打前面用before*/
        content: ' *';
        color: red;
    }
</style>
<body>
<form action="" class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label required">分公司编码</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required|num" name="num" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">分公司名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="name"
                               type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="" name="memo"
                               type="text">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-filter="infosubcompany-save" lay-submit=""
                    type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="layui-btn layui-btn-primary layui-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['form', 'jquery', 'upload'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        var MODULE_PATH = rootPath + "/InfoSubcompanyController/";

        form.verify({
            num: function (value) {
                if (value.length < 2) {
                    return '编号长度需大于2个字符';
                }
                var msg = "";
                $.ajax({
                    url: MODULE_PATH +'checkNumUnique',
                    data: "num=" + value,
                    dataType: 'json',
                    type: 'post',
                    async: false,
                    success: function (result) {
                        msg = result;
                    }
                });
                if (msg == 1) {
                    return "已有相同数据";
                }

            }
        });

        form.on('submit(infosubcompany-save)', function (data) {
            console.log(data.field)
            $.ajax({
                url: MODULE_PATH +'add',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("infoSubcompany-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
</body>
</html>